package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by sahelig on 10/06/14.
 */
public class Library {

    private List<Book> catalogue = new ArrayList<Book>() {{
        add(new Book("Head First Java", "unknown", "2010"));
        add(new Book("Complete Reference to Java", "unknown", "2010"));

    }};
    private List<Movie> catalogueForMovie=new ArrayList<Movie>(){{
        add(new Movie("3 idiots","Unknown","2010",9));
        add(new Movie("Sholay","Unknown","1990",8));
    }};
    private List<Book> checkedOutBooks = new ArrayList<Book>();

    private List<Movie> checkedOutMovies=new ArrayList<Movie>();

    private HashMap<Book,String> peopleCheckingOutBooks=new HashMap<Book, String>();

    private HashMap<Movie,String> peopleCheckingOutMovies=new HashMap<Movie, String>();


    public List<Book> getAvailableBooks()
    {
       List<Book> availableBooks = new ArrayList<Book>();
       availableBooks.addAll(catalogue);
        availableBooks.removeAll(checkedOutBooks);
        return availableBooks;
    }

    public List<Movie> getAvailableMovies() {
        List<Movie> availableMovies = new ArrayList<Movie>();
        availableMovies.addAll(catalogueForMovie);

        availableMovies.removeAll(checkedOutMovies);
        return availableMovies;
    }


    private boolean isBookAvailableInLibrary(Book book)
    {
        List<Book> availableBooks = this.getAvailableBooks();
        int lengthOfAvailableBooks = availableBooks.size();

        for (int i = 0; i < lengthOfAvailableBooks; i++)
        {
            if (availableBooks.get(i).equals(book))
                return true;

        }

        return false;
    }

    private boolean isMovieAvailableInLibrary(Movie movie)
    {
        List<Movie> availableMovies = this.getAvailableMovies();
        int lengthOfAvailableMovies = availableMovies.size();

        for (int i = 0; i < lengthOfAvailableMovies; i++)
        {
            if (availableMovies.get(i).equals(movie))
                return true;

        }

        return false;
    }

    private boolean isACheckedOutBook(Book bookToBeReturned,String libraryNumber)
    {

        for (int i = 0; i < checkedOutBooks.size(); i++)
        {
            if (checkedOutBooks.get(i).equals(bookToBeReturned) && peopleCheckingOutBooks.get(bookToBeReturned).equals(libraryNumber))
                return true;

        }

        return false;
    }
    private boolean isACheckedOutMovie(Movie movieToBeReturned,String libraryNumber)
    {

        for (int i = 0; i < checkedOutMovies.size(); i++)
        {
            if (checkedOutMovies.get(i).equals(movieToBeReturned) && peopleCheckingOutMovies.get(movieToBeReturned).equals(libraryNumber))
                return true;

        }

        return false;
    }


    public Book getBookForInfo(String title, String author, String year)
    {
        int len = catalogue.size();
        for (int i = 0; i < len; i++)
        {
            if (catalogue.get(i).getbookTitle().equalsIgnoreCase(title) && catalogue.get(i).getBookAuthor().equalsIgnoreCase(author) && catalogue.get(i).getBookYear().equalsIgnoreCase(year))
            {
                return catalogue.get(i);
            }
        }
        return null;
    }

    public Movie getMovieForInfo(String title, String director, String year, int rating)
    {
        int len = catalogueForMovie.size();
        for (int i = 0; i < len; i++)
        {
            if (catalogueForMovie.get(i).getMovieTitle().equalsIgnoreCase(title) && catalogueForMovie.get(i).getDirector().equalsIgnoreCase(director) && catalogueForMovie.get(i).getYear().equalsIgnoreCase(year) && catalogueForMovie.get(i).getRating()==rating)
            {
                return catalogueForMovie.get(i);
            }
        }
        return null;
    }


    private void updateCheckedOutList(Book book,String libraryNumber, int action)
    {
        if (action == 0)
        {
            checkedOutBooks.add(book);
            peopleCheckingOutBooks.put(book,libraryNumber);
        }
        else
        {

            checkedOutBooks.remove(book);
            peopleCheckingOutBooks.remove(book);
        }

    }

    private void updateCheckedOutListForMovie(Movie movie,String libraryNumber, int action)
    {
        if (action == 0)
        {
            checkedOutMovies.add(movie);
            peopleCheckingOutMovies.put(movie,libraryNumber);
        }
        else
        {

            checkedOutMovies.remove(movie);
            peopleCheckingOutMovies.remove(movie);
        }

    }


    public boolean returnBook(Book bookToBeReturned,String libraryNumber)
    {
        if(this.isACheckedOutBook(bookToBeReturned,libraryNumber))
        {
            updateCheckedOutList(bookToBeReturned,libraryNumber,1);
            return true;
        }
        return false;
    }

    public boolean returnMovie(Movie movieToBeReturned,String libraryNumber)
    {
        if(this.isACheckedOutMovie(movieToBeReturned, libraryNumber))
        {
            updateCheckedOutListForMovie(movieToBeReturned, libraryNumber, 1);
            return true;
        }
        return false;
    }

    public boolean checkOutBook(Book bookToBeCheckedOut,String libraryNumber)
    {
        if(this.isBookAvailableInLibrary(bookToBeCheckedOut))

        {
            updateCheckedOutList(bookToBeCheckedOut,libraryNumber,0);
            return true;
        }
        return false;

    }
    public boolean checkOutMovie(Movie movieToBeCheckedOut, String libraryNumber)
    {
        if(this.isMovieAvailableInLibrary(movieToBeCheckedOut))
        {
            updateCheckedOutListForMovie(movieToBeCheckedOut,libraryNumber,0);
            return true;
        }
        return false;

    }



}
